package com.yjzx.util.service.pay.config;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.yjzx.util.service.pay.config.properties.WechatProperties;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * @author yjzx
 * @Description
 * @create 2022-12-28 23:20
 */
@Configuration
public class WeixinPayConfig {
    @Bean(name = "wxMaHtmlService")
    public WxMaService wxPayHtmlService(WechatProperties wxPayProperties) {
        WechatProperties.WxApplication application = wxPayProperties.getMini();
        WxMaService maService = new WxMaServiceImpl();
        WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
        config.setAppid(application.getAppId());
        config.setSecret(application.getSecretKey());

        maService.setWxMaConfig(config);
        return maService;

    }
    @Bean(name = "wxPayAppService")
    public WxPayService wxPayAppService(WechatProperties wxPayProperties) {
        WechatProperties.WxApplication app = wxPayProperties.getApp();
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(StringUtils.trimToNull(app.getAppId()));
        payConfig.setMchId(StringUtils.trimToNull(wxPayProperties.getMchId()));
        payConfig.setMchKey(StringUtils.trimToNull(wxPayProperties.getMchKey()));
        payConfig.setNotifyUrl(StringUtils.trimToNull(app.getNotifyUrl()));
        payConfig.setKeyPath(StringUtils.trimToNull(wxPayProperties.getKeyPath()));
        // 可以指定是否使用沙箱环境
        payConfig.setUseSandboxEnv(false);
        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(payConfig);

        return wxPayService;
    }
    @Bean(name = "wxPayMiniService")
    public WxPayService wxPayMiniService(WechatProperties wxPayProperties) {
        WechatProperties.WxApplication mini = wxPayProperties.getMini();
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(StringUtils.trimToNull(mini.getAppId()));
        payConfig.setMchId(StringUtils.trimToNull(wxPayProperties.getMchId()));
        payConfig.setMchKey(StringUtils.trimToNull(wxPayProperties.getMchKey()));
        payConfig.setNotifyUrl(StringUtils.trimToNull(mini.getNotifyUrl()));
        payConfig.setKeyPath(StringUtils.trimToNull(wxPayProperties.getKeyPath()));
        // 可以指定是否使用沙箱环境
        payConfig.setUseSandboxEnv(false);
        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(payConfig);
        return wxPayService;
    }




}
